<script setup>
	import {
		ref
	} from 'vue'
	const url = ref('http://192.168.0.104:5200/')
	const pages = getCurrentPages()
	const vw = ref(null)

	function postMessageToH5(uniCallbackFn, message) {
		vw.value = pages[pages.length - 1].$getAppWebview().children()[0]
		vw.value.evalJS(`${uniCallbackFn}(${JSON.stringify(message)})`)
	}

	function handlerMessage(e) {
		const {
			action,
			message,
			uniCallbackFn
		} = e.detail.data[0]
		if (uni[action]) {

			uni[action]({
				success(res) {
					postMessageToH5(uniCallbackFn, res)
					// TEST
					uni.showModal({
						content: JSON.stringify(res)
					})
				}
			})
		} else {
			// TODO something else
		}
	}
</script>
<template>
	<view>
		<web-view :src="url" @message="handlerMessage" @onPostMessage="handlerMessage"></web-view>
	</view>
</template>